#!/bin/bash

# TSSH software management script
# This script provides functions for managing trzsz-ssh installation

# Default configuration
TSSH_VERSION=${tssh_version:-"0.1.22"}
TSSH_ARCH=$(arch)
TSSH_NAME="tssh_${TSSH_VERSION}_linux_${TSSH_ARCH}"
TSSH_ENV_VAR="TSSH_HOME"
TSSH_DOWNLOAD_PATH="/tmp/tssh-download"
PROXY_PREFIX=${proxy_prefix:-"https://ghproxy.cn/"}

# Get software information
get_software_info() {
    cat << EOF
Software Name: trzsz-ssh (tssh)
Description: SSH client with trzsz (trz/tsz) support for file transfer
Version: ${TSSH_VERSION}
Architecture: ${TSSH_ARCH}
Homepage: https://github.com/trzsz/trzsz-ssh
Installation Path: ${softwares_path}/${TSSH_NAME}
Environment Variable: ${TSSH_ENV_VAR}
EOF
}

# Download and install tssh
execute_install_software() {
    local softwares_path="$1"
    local tssh_home="${softwares_path}/${TSSH_NAME}"
    
    echo "Starting tssh installation..."
    echo "Version: ${TSSH_VERSION}"
    echo "Architecture: ${TSSH_ARCH}"
    echo "Installation path: ${tssh_home}"
    
    # Create download directory
    mkdir -p "${TSSH_DOWNLOAD_PATH}"
    
    # Download tssh
    local download_url="${PROXY_PREFIX}https://github.com/trzsz/trzsz-ssh/releases/download/v${TSSH_VERSION}/${TSSH_NAME}.tar.gz"
    echo "Downloading tssh ${TSSH_VERSION}..."
    echo "Download URL: $download_url"
    curl -C - -o "${TSSH_DOWNLOAD_PATH}/${TSSH_NAME}.tar.gz" -L0 "$download_url"
    
    if [[ $? -ne 0 ]]; then
        echo "Error: Failed to download tssh"
        return 1
    fi
    
    # Create software directory
    sudo mkdir -p "${softwares_path}"
    
    # Remove existing installation if it exists
    if [[ -d "${tssh_home}" ]]; then
        echo "Removing existing installation..."
        sudo rm -rf "${tssh_home}"
    fi
    
    # Extract and install
    echo "Installing tssh to ${tssh_home}..."
    sudo tar -C "${softwares_path}" -xzf "${TSSH_DOWNLOAD_PATH}/${TSSH_NAME}.tar.gz"
    
    if [[ $? -eq 0 ]]; then
        echo "tssh installation completed successfully"
        return 0
    else
        echo "Error: Failed to install tssh"
        return 1
    fi
}

# Set environment configuration
set_rc_config() {
    local rc_path="$1"
    local tssh_home="${softwares_path}/${TSSH_NAME}"
    
    cat << EOF

# ${TSSH_ENV_VAR} configuration
${TSSH_ENV_VAR}="${tssh_home}"
export PATH="\${PATH}:\${${TSSH_ENV_VAR}}"
EOF
}

# Uninstall tssh
execute_uninstall_software() {
    local softwares_path="$1"
    local tssh_home="${softwares_path}/${TSSH_NAME}"
    
    echo "Starting tssh uninstallation..."
    
    # Remove installation directory
    if [[ -d "${tssh_home}" ]]; then
        echo "Removing tssh installation directory: ${tssh_home}"
        sudo rm -rf "${tssh_home}"
    else
        echo "tssh installation directory not found: ${tssh_home}"
    fi
    
    # Remove download cache
    if [[ -d "${TSSH_DOWNLOAD_PATH}" ]]; then
        echo "Cleaning download cache: ${TSSH_DOWNLOAD_PATH}"
        rm -rf "${TSSH_DOWNLOAD_PATH}"
    fi
    
    echo "tssh uninstallation completed"
    return 0
}

# Check if tssh is installed
check_installed() {
    local softwares_path="$1"
    local tssh_home="${softwares_path}/${TSSH_NAME}"
    
    if [[ -d "${tssh_home}" ]] && [[ -x "${tssh_home}/tssh" ]]; then
        echo "tssh is installed at: ${tssh_home}"
        echo "Version: $("${tssh_home}/tssh" --version 2>/dev/null || echo "version info not available")"
        return 0
    else
        echo "tssh is not installed"
        return 1
    fi
}

# Main function for direct script execution
main() {
    if [[ "$1" == "info" ]]; then
        get_software_info
    elif [[ "$1" == "install" ]]; then
        execute_install_software "${softwares_path:-/opt/softwares}"
    elif [[ "$1" == "uninstall" ]]; then
        execute_uninstall_software "${softwares_path:-/opt/softwares}"
    elif [[ "$1" == "check" ]]; then
        check_installed "${softwares_path:-/opt/softwares}"
    else
        echo "Usage: $0 {info|install|uninstall|check}"
        exit 1
    fi
}

# If script is executed directly, run main function
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi